package com.sinodata.bsm.cicp.collector.common;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import com.sinodata.bsm.common.constants.DataTypeConstants;

public class BsmLogin extends AbstractHttpMock {

    @Override
    protected void execute() throws Exception {
        try {
            long t = System.currentTimeMillis();
            GetMethod getMethod = new GetMethod("http://172.168.2.18:8090/bsm/");
            NameValuePair[] pairs = new NameValuePair[] { new NameValuePair("_eventId", "submit"), new NameValuePair("lt", "e1s1"), new NameValuePair("username", "admin"), new NameValuePair("password", "a") };
            client.executeMethod(getMethod);

            getMethod.releaseConnection();
            String url = getMethod.getURI().toString();
            PostMethod postMethod = new PostMethod(url);
            postMethod.setRequestBody(pairs);
            client.executeMethod(postMethod);

            postMethod.releaseConnection();
            url = postMethod.getResponseHeader("location").getValue();
            getMethod = new GetMethod(url);
            client.executeMethod(getMethod);

            long endTime = System.currentTimeMillis() - t;
            this.respondTime = endTime;
            String body = getMethod.getResponseBodyAsString();
            if (body.indexOf("administrator") != -1) {
                this.returnVal = DataTypeConstants.BOOLEAN_VALUE_TRUE;
            } else {
                this.returnVal = DataTypeConstants.BOOLEAN_VALUE_FALSE;
            }

            getMethod.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
